package day12;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * @author oldliu
 * @since 1.0
 */
public class TalkServer {
    public static void main(String[] args) {
        ServerSocket st= null;
        try {
            st = new ServerSocket(9996);
        } catch (IOException e) {
            e.printStackTrace();
            return;
        }
        while (true){
            Socket s =null;
            try {
                s=st.accept();
                System.out.println(s + "链接了");
                InputStream in = s.getInputStream();
                OutputStream out = s.getOutputStream();
                //构建的字符流，为了聊天
                BufferedReader br = new BufferedReader(new InputStreamReader(in));
                PrintWriter pw = new PrintWriter(out, true);
                //客户端问，服务器回答的方式
                String ask = null;
                while (true) {
                    ask = br.readLine();
                    if(ask==null)
                        break;
                    System.out.println("问：" + ask);
                    pw.println("你的问题【" + ask + "】我收到了，稍后联系你");
                    //>>>pw.flush();
                    if ("byebye".equals(ask))
                        break;
                }
            }catch (Exception e){
                e.printStackTrace();
            }finally {
                try {
                    s.close();
                    System.out.println(s+"关闭了");
                } catch (Exception e) {

                }
            }

        }
    }
}

